package cn.gzjp.modules.it.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.it.entity.Car;
import cn.gzjp.modules.it.entity.Member;
import cn.gzjp.modules.it.entity.MemberCar;
import cn.gzjp.modules.it.dao.MemberDao;

/**
 * 会员Service
 * @author zengcp
 * @version 2018-01-26
 */
@Service
@Transactional(readOnly = true)
public class MemberService extends CrudService<MemberDao, Member> {
	
	@Autowired
	private MemberCarService memberCarService;
	@Autowired
	private CarService carService;
	
	@Transactional(readOnly = false)
	public Member get(String id) {
		MemberCar memberCar = new MemberCar();
		memberCar.setMemberId(id);
		List<MemberCar> mcarList = memberCarService.findList(memberCar);
		List<Car> carList = Lists.newArrayList();
		for(MemberCar mc:mcarList){
			Car car = carService.get(mc.getCarId());
			carList.add(car);
		}
		Member member = super.get(id);
		if(member!=null&&carList!=null){
			member.setCarAmount(carList.size());//设置绑定车辆数
			member.setCarList(carList);
			this.dao.update(member);
		}
		return member;
	}
	
	public List<Member> findList(Member member) {
		return super.findList(member);
	}
	
	public Page<Member> findPage(Page<Member> page, Member member) {
		return super.findPage(page, member);
	}
	
	@Transactional(readOnly = false)
	public void save(Member member) {
		Member m = get(member.getId());
		if(m!=null){
			super.save(member);
		}else{
			super.insert(member, member.getId());
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Member member) {
		super.deleteByLogic(member);
	}
	
}